package com.daqian.cloud.oauth2.controller;

import com.daqian.cloud.oauth2.repository.MemberRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * 用户信息控制器
 */
@RestController
@RequestMapping(value = "/oauth")
public class MemberController {

    @Autowired
    ConsumerTokenServices consumerTokenServices;

    /**
     * 账户数据操作接口
     */
    @Autowired
    private MemberRepository memberRepository;

    /**
     * 获取授权用户的信息
     * @param user 当前用户
     * @return 授权信息
     */
    @GetMapping("/user")
    public ResponseEntity user(Principal user) {
        return ResponseEntity.ok("这里是用户哇");
    }

    /**
     * 重写授权异常处理方法
     * @param user
     * @return
     */
    @GetMapping("/error")
    public ResponseEntity aaa(Principal user) {
        System.out.println("dddfdf");
        return ResponseEntity.ok("dddfdfdddfdf");
    }

    /**
     * 废除token
     * @param access_token
     * @return
     */
    @GetMapping("/logout")
    public ResponseEntity revokeToken(String access_token) {
        if (consumerTokenServices.revokeToken(access_token)) {
            return ResponseEntity.ok("注销成功");
        } else {
            return ResponseEntity.ok("注销失败");
        }
    }
}
